package com.ellecity06.cwhiskymall.ui.contribution

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.ellecity06.common.base.BaseMvvmActivity
import com.ellecity06.common.ktx.observeEvent
import com.ellecity06.common.ktx.statusBar
import com.ellecity06.cwhiskymall.bean.response.UserAccountResponse
import com.ellecity06.cwhiskymall.databinding.ActivityContributionBinding
import com.ellecity06.cwhiskymall.ui.webview.WebHelper
import com.gyf.immersionbar.ktx.immersionBar

/**
 * @author ellecity06
 * @time 2024/10/29 15:26
 * @des
 */
class ContributionActivity : BaseMvvmActivity<ActivityContributionBinding, ContributionViewModel>(),
    View.OnClickListener {
    override val userBaseTitle: Boolean
        get() = false

    override fun inflateBinding(): ActivityContributionBinding {
        return ActivityContributionBinding.inflate(layoutInflater)

    }

    override fun initialize(savedInstanceState: Bundle?) {
        statusBar(binding.statusBar)
        binding.ivBack.setOnClickListener(this)
        fetchData()

    }

    override fun initEvent() {
        binding.layoutCoverRecord.setOnClickListener(this)
        binding.layoutCoverApp.setOnClickListener(this)
        binding.viewLeft.setOnClickListener(this)
        binding.viewRight.setOnClickListener(this)
    }

    private fun fetchData() {
        lifecycleScope.launchWhenCreated {
            viewModel.intentChannel.send(ContributionIntent.FetchData)
        }
    }

    override fun initObserver() {
        viewModel.state.observeEvent(this) {
            when (it) {
                is ContributionState.AcountInfo -> {
                    setAccountInfo(it.data)

                }

                else -> {}
            }
        }
    }

    private fun setAccountInfo(data: UserAccountResponse) {
        // 可兑换的贡献金
        binding.tvContributionAmount.text = data.exchangeContribute
        // 冻结的贡献金
        binding.tvFirstOrderAmount.text = data.frozenContribute
    }

    override fun initImmersionBar() {
        immersionBar {
            titleBar(binding.toolBar)
            statusBarDarkFont(false)
        }
    }

    override fun onClick(view: View?) {
        when (view?.id) {
            binding.ivBack.id -> {
                finish()
            }

            binding.layoutCoverRecord.id -> {
                WebHelper.toRedeemRecord(this)
            }

            binding.layoutCoverApp.id -> {
                WebHelper.toRedeem(this)
            }

            binding.viewLeft.id -> {
                WebHelper.toAssetsRecord(this)
            }

            binding.viewRight.id -> {
                WebHelper.toAssetsRecord2(this)
            }
        }
    }
}